package com.example.final_project.Usr.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.final_project.Home.Activity.CarNewsActivity;
import com.example.final_project.R;
import com.example.final_project.Usr.Activity.ShopActivity;
import com.example.final_project.Usr.entry.UserStore;

import java.util.List;

public class ShopAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<UserStore> shops;
    private ImageView img;
    private TextView name,price;

    public ShopAdapter(Context context, List<UserStore> shops){
        this.context = context;
        this.shops = shops;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.userstore_kids,parent,false);
        ShopHolder shopHolder = new ShopHolder(view);
        return shopHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ShopHolder shopHolder = (ShopHolder)holder;
        shopHolder.setData(position);
    }

    @Override
    public int getItemCount() {
        return shops.size();
    }

    class ShopHolder extends RecyclerView.ViewHolder {
        public ShopHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setData(int position){
            UserStore userStore = shops.get(position);
            img = itemView.findViewById(R.id.shop_img);
            Glide.with(context).load("http://10.150.11.9:8086/chengcar/banners/"+userStore.getImg()).into(img);
            name = itemView.findViewById(R.id.shop_name);
            name.setText(userStore.getName());
            price = itemView.findViewById(R.id.shop_price);
            price.setText(userStore.getPrice());

            //商品详情页面
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, ShopActivity.class);
                    intent.putExtra("id",userStore.getId());
                    context.startActivity(intent);
                }
            });
        }

    }

}
